package leecode

import "math"

func divide(a int, b int) int {
	if a == math.MinInt32 && b == -1 {
		return math.MaxInt32
	}
	if a == 0 {
		return 0
	}
	//确定结果符号
	flag := -1
	var result int
	if (a > 0 && b > 0) || (a < 0 && b < 0) {
		flag = 1 //正数结果
	}
	a = abs1(a)
	b = abs1(b)
	for a >= b {
		multiple := 1
		temp := b
		for b >= temp*2 {
			temp = temp * 2
			multiple = multiple * 2
		}
		a = a - temp
		result += multiple
	}
	if flag == -1 {
		return -result
	}
	return result
}

func abs1(a int) int {
	if a < 0 {
		return -a
	}
	return a
}
